﻿using App.DbContexts;
using App.UnitOfWork;
using App.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace App.Business
{
    public class ShareBusiness
    {

        public IRepository<ShareEntity> _shareRepository { get; set; }
        public IRepository<MyShareEntity> _myShareRepository { get; set; }
        public IRepository<ShareOnTimeEntity> _shareOnTimeRepository { get; set; }

        public void Init()
        {
            _shareRepository.Insert(new ShareEntity()
            {
                Code = "sh600900",
                Name = "长江电力",
                Today = DateTime.Now.ToString("yyyy-MM-dd"),
                Trend = TrendType.趋势上涨,
                Suggest = String.Empty,
            });
        }

        public void GetNow()
        {
            var shares = _shareRepository.QueryAble().ToList();
            var headers = new Dictionary<string, string>();
            headers.Add("Referer", "http://finance.sina.com.cn");
            foreach (var share in shares)
            {
                var result = HttpHelper.HttpGet($"http://hq.sinajs.cn/list={share.Code}", null, headers);

                if (result.IsError)
                    return;

                var array = result.Data.Split(",");

                _shareOnTimeRepository.Insert(new ShareOnTimeEntity()
                {
                    Code = share.Code,
                    DateTime = DateTime.Parse($"{array[30]} {array[31]}"),
                    Price = double.Parse(array[3].ToString())
                });
            }
        }

    }
}
